package com.guojiaming.shop.controller;

import com.guojiaming.shop.bean.*;
import com.guojiaming.shop.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author vivo
 */
@RestController
@RequestMapping("user")
public class UseController {
    @Resource
    private UserService userService;

    @GetMapping("all")
    public ResponseBody<ListBody<User>> getAll() {
        return ResponseFactory.list(userService.findAll());
    }

    @GetMapping("save")
    public ResponseBody<User> saveUser(@Valid User user) {
        return ResponseFactory.success(userService.addUser(user));
    }

    @GetMapping("delete")
    public ResponseBody<Integer> deleteUser(@Valid @RequestParam(value = "userId") Integer userId) {
        return ResponseFactory.success(userService.deleteUser(userId), "删除成功");
    }

    @GetMapping("update")
    public ResponseBody<User> updateUser(@Valid UserModity userModity) {
        return ResponseFactory.success(userService.updateUser(userModity), "修改成功");
    }

    @GetMapping("find")
    public ResponseBody<User> selectUser(@Valid @RequestParam(value = "userId") Integer userId) {
        return ResponseFactory.success(userService.selectUser(userId));
    }

    @GetMapping("modifyPass")
    public ResponseBody<Boolean> modifyPass(@Valid UserModifyPass userModifyPass) {
        return ResponseFactory.success(userService.modifyPass(userModifyPass), "修改成功");
    }

}
